using System;
using System.Diagnostics;
using System.Drawing;
using System.ComponentModel;
using System.Web;
using System.Data;
using System.Text;

namespace OR.Reuse.Persistence
{
	/// <summary>
	/// Summary description for DataConverter.
	/// </summary>
	public class DataConverter
	{
		public static bool ToBool( string p_szSource )
		{
			return( p_szSource == "1" ? true : false );
		}

		/// <summary>
		/// Convert Hex Color to RGB and then System.Drawing.Color
		/// </summary>
		/// <param name="p_szValue">Hex Color</param>
		/// <returns>System.Drawing.Color</returns>
		public static Color ToColor( string p_szValue )
		{           
			return ( ColorTranslator.FromHtml( p_szValue ) );
		}
      
		public static String HtmlEncode( string p_szValue )
		{
			return ( HttpUtility.HtmlEncode( p_szValue ) );
		}

		public static String HtmlDecode( string p_szValue )
		{
			return ( HttpUtility.HtmlDecode( p_szValue ) );
		}

		public static String ToPageFile( string p_szValue )
		{
			string   v_szResult  = p_szValue;
			int      v_iPosition = p_szValue.LastIndexOf( "_" );

			v_szResult = v_szResult.Substring( 0, v_iPosition ) + ".aspx";

			return( v_szResult );
		}

		public static string ToCSV( object[] pSource, string pSeparator, string pIdentifier )
		{
			string vResult = "";

			foreach( object vCurrent in pSource )
			{
				if ( !DataUtility.IsEmpty( vCurrent ) )
				{
					vResult += pIdentifier + vCurrent.ToString() + pIdentifier + pSeparator;
				}
			}

			if ( vResult != "" )
			{
				vResult = vResult.Substring( 0, vResult.Length - 1 );
			}

			return( vResult );
		}

		public static string ToCSV( int[] pSource, string pSeparator, string pIdentifier )
		{
			string vResult = "";

			if ( pSource != null )
			{
				foreach( int vCurrent in pSource )
				{
					if ( !DataUtility.IsEmpty( vCurrent ) )
					{
						vResult += pIdentifier + vCurrent.ToString() + pIdentifier + pSeparator;
					}
				}
			}

			if ( vResult != "" )
			{
				vResult = vResult.Substring( 0, vResult.Length - 1 );
			}

			return( vResult );
		}

		public static string ToCSV( string[] p_arrSource, string p_szSeparator, string p_szIdentifier )
		{
			string v_szResult = "";

			foreach( string v_szCurrent in p_arrSource )
			{
				if ( v_szCurrent != null )
				{
					v_szResult += p_szIdentifier + v_szCurrent + p_szIdentifier + p_szSeparator;
				}
			}

			if ( v_szResult != "" )
			{
				v_szResult = v_szResult.Substring( 0, v_szResult.Length - 1 );
			}

			return( v_szResult );
		}

		public static string ToCSV( Guid[] p_arrSource, string p_szSeparator, string p_szIdentifier )
		{
			string v_szResult = "";

			foreach( Guid v_uCurrent in p_arrSource )
			{
				if ( v_uCurrent != Guid.Empty )
				{
					v_szResult += p_szIdentifier + v_uCurrent.ToString().ToUpper() + p_szIdentifier + p_szSeparator;
				}
			}

			if ( v_szResult != "" )
			{
				v_szResult = v_szResult.Substring( 0, v_szResult.Length - 1 );
			}

			return( v_szResult );
		}

		public static string ToCSV( DataView p_dvSource, string p_szColumn )
		{
			Guid[]	v_arrTarget;
			
			DataConverter.ToArray( out v_arrTarget, p_dvSource, p_szColumn );

			return( ToCSV( v_arrTarget, ",", "" ) );
		}

		public static string ToQuotedString( string pSource, string pQuote, string pType )
		{
			string	vReturn		= String.Empty;
			bool		bIsString	= ( pType == "System.String" );

			if ( bIsString )
			{
				vReturn += pQuote;
			}

			vReturn += pSource;

			if ( bIsString )
			{
				vReturn += pQuote;
			}

			return ( vReturn );
		}

		public static string ToSQLSafeString( string pSource )
		{
			string vReturn = null;

			vReturn = pSource.Replace( "'", "''" );
			
			return vReturn;
		}

		public static void ToArray( out Guid[] p_arrTarget, DataTable p_dtSource, string p_szColumn )
		{
			p_arrTarget = new Guid[ p_dtSource.Rows.Count ];
			int   v_iCounter  = 0;

			foreach( DataRow v_drCurrent in p_dtSource.Rows )
			{
				p_arrTarget[ v_iCounter ] = DataUtility.GetGuid( v_drCurrent, p_szColumn );

				v_iCounter++;
			}
		}

		public static void ToArray( out Guid[] p_arrTarget, DataView p_dvSource, string p_szColumn )
		{
			p_arrTarget = new Guid[ p_dvSource.Count ];
			int   v_iCounter  = 0;

			foreach( DataRowView v_drvCurrent in p_dvSource )
			{
				p_arrTarget[ v_iCounter ] = DataUtility.GetGuid( v_drvCurrent.Row, p_szColumn );

				v_iCounter++;
			}
		}

		public static string ToHexColor( Color pValue )
		{
			return ( ColorTranslator.ToHtml( pValue ) );
		}

		public static string ToWebColor( Color pValue )
		{
			return ( ColorTranslator.ToHtml( pValue ) );
		}

		public static string FileUrlEncode( string pSource )
		{
			string[]	vPieces	= pSource.Split( new char[] { '\\' } );
			string	vPiece	= String.Empty;
			string	vReturn	= String.Empty;
			int		vCounter	= 0;

			while ( vCounter < vPieces.Length )
			{
				vPiece = vPieces[ vCounter ];

				vReturn += vPiece.Replace( " ", "%20d" );

				if ( vCounter < vPieces.Length - 1 )
				{
					vReturn += "\\";
				}

				vCounter++;
			}

			return ( vReturn );
		}

		public static object ToDecimal( object pValue )
		{
			object	vReturn = null;

			if ( pValue is System.String )
			{
				string	vValue = ( String )pValue;
				
				vValue	= vValue.Replace( "$", String.Empty );
				vValue	= vValue.Replace( "(", "-" );
				vValue	= vValue.Replace( "- ", "-" );
				vValue	= vValue.Replace( ",", String.Empty );
				vValue	= vValue.Replace( ")", String.Empty );

				vReturn = Convert.ToDecimal( vValue );
			}
			else if ( ( pValue != null ) && ( pValue != DBNull.Value ) )
			{
				vReturn = Convert.ToDecimal( pValue );
			}

			return ( vReturn );
		}

		public static object ToInt32( object pValue )
		{
			object	vReturn = null;

			if ( pValue is System.String )
			{
				string	vValue = ( String )pValue;
				
				vValue	= vValue.Replace( "(", "-" );
				vValue	= vValue.Replace( "- ", "-" );
				vValue	= vValue.Replace( ",", String.Empty );
				vValue	= vValue.Replace( ")", String.Empty );

				vReturn = Convert.ToInt32( vValue );
			}
			else if ( ( pValue != null ) && ( pValue != DBNull.Value ) )
			{
				vReturn = Convert.ToInt32( pValue );
			}

			return ( vReturn );
		}

		public static object GetStrongType( object pValue, string pType )
		{
			object	vReturn = null;

			try
			{
				switch ( pType )
				{
					case "System.Int32":
					{
						vReturn = DataConverter.ToInt32( pValue );

						break;
					}
					case "System.Decimal":
					{
						vReturn = DataConverter.ToDecimal( pValue );

						break;
					}
					case "System.DateTime":
					{
						vReturn = Convert.ToDateTime( pValue );

						break;
					}
					default:
					{
						vReturn = pValue;

						break;
					}
				}
			}
			catch ( Exception ex )
			{
				throw new ApplicationException( "DataConverter.GetStrongType( object, string ) failed: The value " + pValue.ToString() + " cannot be converted to type " + pType + ".", ex );
			}

			return ( vReturn );
		}

		private DataConverter()
		{
			//
			// TODO: Add constructor logic here
			//
		}	
	}
}
